﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using BangleCMS.DBUtility;//Please add references
namespace BangleCMS.DAL
{
	/// <summary>
	/// 数据访问类:t_productDAL
	/// </summary>
	public partial class t_productDAL
	{
		public t_productDAL()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperMySQL.GetMaxID("ProductId", "t_product"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ProductId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from t_product");
			strSql.Append(" where ProductId=@ProductId");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ProductId", MySqlDbType.Int32)
			};
			parameters[0].Value = ProductId;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(BangleCMS.Model.t_product model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into t_product(");
			strSql.Append("Title,Keywords,Description,ReleaseDate,Source,Author,LogoImg,ProductType,Abstract,Content,ReadCount,OrderNo,Top,CreateUser,CreateDate,UpdateUser,UpdateDate,Valid)");
			strSql.Append(" values (");
			strSql.Append("@Title,@Keywords,@Description,@ReleaseDate,@Source,@Author,@LogoImg,@ProductType,@Abstract,@Content,@ReadCount,@OrderNo,@Top,@CreateUser,@CreateDate,@UpdateUser,@UpdateDate,@Valid)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@Title", MySqlDbType.VarChar,100),
					new MySqlParameter("@Keywords", MySqlDbType.VarChar,500),
					new MySqlParameter("@Description", MySqlDbType.VarChar,500),
					new MySqlParameter("@ReleaseDate", MySqlDbType.DateTime),
					new MySqlParameter("@Source", MySqlDbType.VarChar,50),
					new MySqlParameter("@Author", MySqlDbType.VarChar,50),
					new MySqlParameter("@LogoImg", MySqlDbType.VarChar,50),
					new MySqlParameter("@ProductType", MySqlDbType.VarChar,50),
					new MySqlParameter("@Abstract", MySqlDbType.VarChar,500),
					new MySqlParameter("@Content", MySqlDbType.VarChar,10000),
					new MySqlParameter("@ReadCount", MySqlDbType.Int32,11),
					new MySqlParameter("@OrderNo", MySqlDbType.Int32,11),
					new MySqlParameter("@Top", MySqlDbType.Int32,11),
					new MySqlParameter("@CreateUser", MySqlDbType.Int32,11),
					new MySqlParameter("@CreateDate", MySqlDbType.DateTime),
					new MySqlParameter("@UpdateUser", MySqlDbType.Int32,11),
					new MySqlParameter("@UpdateDate", MySqlDbType.DateTime),
					new MySqlParameter("@Valid", MySqlDbType.Int32,11)};
			parameters[0].Value = model.Title;
			parameters[1].Value = model.Keywords;
			parameters[2].Value = model.Description;
			parameters[3].Value = model.ReleaseDate;
			parameters[4].Value = model.Source;
			parameters[5].Value = model.Author;
			parameters[6].Value = model.LogoImg;
			parameters[7].Value = model.ProductType;
			parameters[8].Value = model.Abstract;
			parameters[9].Value = model.Content;
			parameters[10].Value = model.ReadCount;
			parameters[11].Value = model.OrderNo;
			parameters[12].Value = model.Top;
			parameters[13].Value = model.CreateUser;
			parameters[14].Value = model.CreateDate;
			parameters[15].Value = model.UpdateUser;
			parameters[16].Value = model.UpdateDate;
			parameters[17].Value = model.Valid;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(BangleCMS.Model.t_product model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_product set ");
			strSql.Append("Title=@Title,");
			strSql.Append("Keywords=@Keywords,");
			strSql.Append("Description=@Description,");
			strSql.Append("ReleaseDate=@ReleaseDate,");
			strSql.Append("Source=@Source,");
			strSql.Append("Author=@Author,");
			strSql.Append("LogoImg=@LogoImg,");
			strSql.Append("ProductType=@ProductType,");
			strSql.Append("Abstract=@Abstract,");
			strSql.Append("Content=@Content,");
			strSql.Append("ReadCount=@ReadCount,");
			strSql.Append("OrderNo=@OrderNo,");
			strSql.Append("Top=@Top,");
			strSql.Append("CreateUser=@CreateUser,");
			strSql.Append("CreateDate=@CreateDate,");
			strSql.Append("UpdateUser=@UpdateUser,");
			strSql.Append("UpdateDate=@UpdateDate,");
			strSql.Append("Valid=@Valid");
			strSql.Append(" where ProductId=@ProductId");
			MySqlParameter[] parameters = {
					new MySqlParameter("@Title", MySqlDbType.VarChar,100),
					new MySqlParameter("@Keywords", MySqlDbType.VarChar,500),
					new MySqlParameter("@Description", MySqlDbType.VarChar,500),
					new MySqlParameter("@ReleaseDate", MySqlDbType.DateTime),
					new MySqlParameter("@Source", MySqlDbType.VarChar,50),
					new MySqlParameter("@Author", MySqlDbType.VarChar,50),
					new MySqlParameter("@LogoImg", MySqlDbType.VarChar,50),
					new MySqlParameter("@ProductType", MySqlDbType.VarChar,50),
					new MySqlParameter("@Abstract", MySqlDbType.VarChar,500),
					new MySqlParameter("@Content", MySqlDbType.VarChar,10000),
					new MySqlParameter("@ReadCount", MySqlDbType.Int32,11),
					new MySqlParameter("@OrderNo", MySqlDbType.Int32,11),
					new MySqlParameter("@Top", MySqlDbType.Int32,11),
					new MySqlParameter("@CreateUser", MySqlDbType.Int32,11),
					new MySqlParameter("@CreateDate", MySqlDbType.DateTime),
					new MySqlParameter("@UpdateUser", MySqlDbType.Int32,11),
					new MySqlParameter("@UpdateDate", MySqlDbType.DateTime),
					new MySqlParameter("@Valid", MySqlDbType.Int32,11),
					new MySqlParameter("@ProductId", MySqlDbType.Int32,11)};
			parameters[0].Value = model.Title;
			parameters[1].Value = model.Keywords;
			parameters[2].Value = model.Description;
			parameters[3].Value = model.ReleaseDate;
			parameters[4].Value = model.Source;
			parameters[5].Value = model.Author;
			parameters[6].Value = model.LogoImg;
			parameters[7].Value = model.ProductType;
			parameters[8].Value = model.Abstract;
			parameters[9].Value = model.Content;
			parameters[10].Value = model.ReadCount;
			parameters[11].Value = model.OrderNo;
			parameters[12].Value = model.Top;
			parameters[13].Value = model.CreateUser;
			parameters[14].Value = model.CreateDate;
			parameters[15].Value = model.UpdateUser;
			parameters[16].Value = model.UpdateDate;
			parameters[17].Value = model.Valid;
			parameters[18].Value = model.ProductId;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ProductId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_product ");
			strSql.Append(" where ProductId=@ProductId");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ProductId", MySqlDbType.Int32)
			};
			parameters[0].Value = ProductId;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string ProductIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_product ");
			strSql.Append(" where ProductId in ("+ProductIdlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BangleCMS.Model.t_product GetModel(int ProductId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ProductId,Title,Keywords,Description,ReleaseDate,Source,Author,LogoImg,ProductType,Abstract,Content,ReadCount,OrderNo,Top,CreateUser,CreateDate,UpdateUser,UpdateDate,Valid from t_product ");
			strSql.Append(" where ProductId=@ProductId");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ProductId", MySqlDbType.Int32)
			};
			parameters[0].Value = ProductId;

			BangleCMS.Model.t_product model=new BangleCMS.Model.t_product();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ProductId"]!=null && ds.Tables[0].Rows[0]["ProductId"].ToString()!="")
				{
					model.ProductId=int.Parse(ds.Tables[0].Rows[0]["ProductId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Title"]!=null && ds.Tables[0].Rows[0]["Title"].ToString()!="")
				{
					model.Title=ds.Tables[0].Rows[0]["Title"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Keywords"]!=null && ds.Tables[0].Rows[0]["Keywords"].ToString()!="")
				{
					model.Keywords=ds.Tables[0].Rows[0]["Keywords"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Description"]!=null && ds.Tables[0].Rows[0]["Description"].ToString()!="")
				{
					model.Description=ds.Tables[0].Rows[0]["Description"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ReleaseDate"]!=null && ds.Tables[0].Rows[0]["ReleaseDate"].ToString()!="")
				{
					model.ReleaseDate=DateTime.Parse(ds.Tables[0].Rows[0]["ReleaseDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Source"]!=null && ds.Tables[0].Rows[0]["Source"].ToString()!="")
				{
					model.Source=ds.Tables[0].Rows[0]["Source"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Author"]!=null && ds.Tables[0].Rows[0]["Author"].ToString()!="")
				{
					model.Author=ds.Tables[0].Rows[0]["Author"].ToString();
				}
				if(ds.Tables[0].Rows[0]["LogoImg"]!=null && ds.Tables[0].Rows[0]["LogoImg"].ToString()!="")
				{
					model.LogoImg=ds.Tables[0].Rows[0]["LogoImg"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ProductType"]!=null && ds.Tables[0].Rows[0]["ProductType"].ToString()!="")
				{
					model.ProductType=ds.Tables[0].Rows[0]["ProductType"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Abstract"]!=null && ds.Tables[0].Rows[0]["Abstract"].ToString()!="")
				{
					model.Abstract=ds.Tables[0].Rows[0]["Abstract"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Content"]!=null && ds.Tables[0].Rows[0]["Content"].ToString()!="")
				{
					model.Content=ds.Tables[0].Rows[0]["Content"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ReadCount"]!=null && ds.Tables[0].Rows[0]["ReadCount"].ToString()!="")
				{
					model.ReadCount=int.Parse(ds.Tables[0].Rows[0]["ReadCount"].ToString());
				}
				if(ds.Tables[0].Rows[0]["OrderNo"]!=null && ds.Tables[0].Rows[0]["OrderNo"].ToString()!="")
				{
					model.OrderNo=int.Parse(ds.Tables[0].Rows[0]["OrderNo"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Top"]!=null && ds.Tables[0].Rows[0]["Top"].ToString()!="")
				{
					model.Top=int.Parse(ds.Tables[0].Rows[0]["Top"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateUser"]!=null && ds.Tables[0].Rows[0]["CreateUser"].ToString()!="")
				{
					model.CreateUser=int.Parse(ds.Tables[0].Rows[0]["CreateUser"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateDate"]!=null && ds.Tables[0].Rows[0]["CreateDate"].ToString()!="")
				{
					model.CreateDate=DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UpdateUser"]!=null && ds.Tables[0].Rows[0]["UpdateUser"].ToString()!="")
				{
					model.UpdateUser=int.Parse(ds.Tables[0].Rows[0]["UpdateUser"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UpdateDate"]!=null && ds.Tables[0].Rows[0]["UpdateDate"].ToString()!="")
				{
					model.UpdateDate=DateTime.Parse(ds.Tables[0].Rows[0]["UpdateDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Valid"]!=null && ds.Tables[0].Rows[0]["Valid"].ToString()!="")
				{
					model.Valid=int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ProductId,Title,Keywords,Description,ReleaseDate,Source,Author,LogoImg,ProductType,Abstract,Content,ReadCount,OrderNo,Top,CreateUser,CreateDate,UpdateUser,UpdateDate,Valid ");
			strSql.Append(" FROM t_product ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM t_product ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ProductId desc");
			}
			strSql.Append(")AS Row, T.*  from t_product T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "t_product";
			parameters[1].Value = "ProductId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

